/*****************************************************************************
*
* File Name : wm_spi_codec_demo.c
*
* Description: ntp demo function
*
* Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd.
* All rights reserved.
*
* Author : dave
*
* Date : 2014-10-28
*****************************************************************************/
#include "wm_include.h"
#include "wm_demo.h"
#include "wm_dlna_dmr.h"
#include <string.h>

#if DEMO_DLNA_DMR

#define  SPI_CLK  8   //CTS
#define  SPI_DATO  9   //RTS
#define  SPI_CCS    11
#define  DAT_REQ    3
#define  LED        4
#define  RST        5
#define  RUN_MODE       6


#define SPI_CK(x)  tls_gpio_write(SPI_CLK,x)
#define SPI_DO(x)  tls_gpio_write(SPI_DATO,x)
#define SPI_CS(x)  tls_gpio_write(SPI_CCS,x)

#define LED_ON(x)  tls_gpio_write(LED,x)

#define AUDIO_RST(x)  tls_gpio_write(RST,x)

#define    MUSIC_BUF_MAX_INDX     40
#define    HTTP_CLIENT_BUFFER_SIZE   512
#define    MUSI_BUF_SIZE  (HTTP_CLIENT_BUFFER_SIZE*MUSIC_BUF_MAX_INDX)

static tls_os_queue_t * sd_down_mbox = NULL;
#define UPNP_SD_STK_SIZE  500 //spi write down
OS_STK         sd_down_task_stk[UPNP_SD_STK_SIZE];

u32 SendCnt;
u32 writeCnt;
u32 DownSize;
char  MusicData[MUSI_BUF_SIZE];

#if 0
char MUSIC_DATA[2673] = {
	0xFF, 0xE2, 0x2B, 0x74, 0xD5, 0xFD, 0x00, 0x02, 0x58, 0x2A, 0xFF, 0x20, 0xA1, 0x00, 0x00, 0x83,
	0x30, 0x05, 0xF9, 0x75, 0x40, 0x00, 0x00, 0x01, 0x49, 0x20, 0x00, 0x7F, 0xF9, 0xE8, 0x42, 0x40,
	0x04, 0x30, 0x78, 0x7F, 0x03, 0xF0, 0x00, 0x76, 0x0E, 0xCC, 0x10, 0xFF, 0xE2, 0x26, 0x5D, 0x40,
	0x03, 0xE5, 0xDE, 0x8F, 0xCA, 0x41, 0xFF, 0x81, 0x1C, 0x30, 0xC3, 0x08, 0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, 0xFF, 0xFF, 0xFF, 0xFF,
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
	0xE2, 0x2B, 0x54, 0xDD, 0x33, 0xFF, 0x0E, 0xE0, 0x01, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA8,
	0x02, 0x5C, 0xAC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

};

#endif
static void spi_write_byte(u8 out)
{
	unsigned char i = 0;
	for (i = 0; i < 8; i++)
	{
		SPI_CK(0);
		SPI_CK(0);
		SPI_CK(0);
		SPI_CK(0);
		SPI_CK(0);
		SPI_CK(0);     //延时用

		if ((out & 0x80) ) SPI_DO(1);
		else			  SPI_DO(0);

		SPI_CK(0);
	//	SPI_CK(0);
	//	SPI_CK(0);
//		SPI_CK(0);
		//SPI_CK(0);     //延时用
		out <<= 1;
		SPI_CK(1);
		SPI_CK(1);//做延时用
		SPI_CK(1);
		SPI_CK(1);
		SPI_CK(1);
		SPI_CK(1);
		SPI_CK(1);

	}


}

static void spi_send_audio(char * buf,int len )
{
	int  i;
	for(i = 0;i<len;i++)
	{
		spi_write_byte(*buf++);
	}
}


static void download_finish_callback(char * buf, int datalen)
{
	int pos = writeCnt%MUSI_BUF_SIZE;
	memcpy(&MusicData[pos], buf, datalen);
	writeCnt += datalen;
}
static void spitocodec(void)
{
	int index;
	int pos;

	if(writeCnt>SendCnt)
	{
		index=SendCnt/HTTP_CLIENT_BUFFER_SIZE;
		index%=MUSIC_BUF_MAX_INDX;
		pos=index*HTTP_CLIENT_BUFFER_SIZE;
		//printf("\ns:%x %x %x ",MusicData[pos],MusicData[pos+1],MusicData[pos+2]);
		SPI_CS(0);
		//tls_os_time_delay(1);
		delay_us(100);
		//printf("writeCnt=%d, SendCnt=%d, pos=%d\n",writeCnt,SendCnt,pos);
		spi_send_audio(MusicData+pos,HTTP_CLIENT_BUFFER_SIZE);
		SPI_CS(1);
		SendCnt+=HTTP_CLIENT_BUFFER_SIZE;
		DownSize=HTTP_CLIENT_BUFFER_SIZE;
		//这个地方应该调用http中的api，启动下载
		tls_dmr_download_data(DownSize, download_finish_callback);
	}
	else
		printf("no cache data!\n");
}

static void codec_isr_callback(void *context)
{
	u16 ret;

	ret = tls_get_gpio_int_flag(DAT_REQ);

	if(ret)
	{
		tls_clr_gpio_int_flag(DAT_REQ);
		tls_os_queue_send(sd_down_mbox, (void *)0, 0);
	}
}


static void codec_isr_init(void)
{
	u16 gpio_pin;

	gpio_pin = DAT_REQ;

	tls_gpio_cfg(gpio_pin, TLS_GPIO_DIR_INPUT, TLS_GPIO_ATTR_PULLHIGH);
	tls_gpio_isr_register(gpio_pin, codec_isr_callback, NULL);
	tls_gpio_int_enable(gpio_pin, TLS_GPIO_INT_TRIG_FALLING_EDGE);
}
static void httpstopdownloadmusic()
{
	tls_gpio_int_disable(DAT_REQ);
}

static void first_download_finish_callback(char * buf, int datalen)
{
	int pos = writeCnt%MUSI_BUF_SIZE;
	memcpy(&MusicData[pos], buf, datalen);
	writeCnt += datalen;

	if(writeCnt<MUSI_BUF_SIZE && datalen > 0)
	{
		tls_dmr_download_data(MUSI_BUF_SIZE, first_download_finish_callback);
		return;
	}

	codec_isr_init();
	AUDIO_RST(0);
	delay_us(200);
	AUDIO_RST(1);
}

static void httpdownloadmusic()
{
	//printf("httpdownloadmusic enter\n");
	httpstopdownloadmusic();
	writeCnt = 0;
	SendCnt = 0;
	tls_dmr_download_data(MUSI_BUF_SIZE, first_download_finish_callback);
}

static float get_grogress(int totlen)
{
	//printf("SendCnt %d totlen %d\n", SendCnt, totlen);
	return ((float)SendCnt) / totlen;
}

static void
sd_down_thread(void *arg)
{
	void * msg;
	while(TRUE)
	{
		tls_os_queue_receive(sd_down_mbox, &msg, 0, 0);
		spitocodec();
	}
}

static void mute_callback(enum dmr_control_type type, const char* channel, int* value)
{
	printf("mute callback : type = %s , channel = %s, value = %d\n", (type == 0 ? "GET" : "SET"), channel, *value);
}
static void volume_callback(enum dmr_control_type type, const char* channel, int* value)
{
	printf("volume callback : type = %s , channel = %s, value = %d\n", (type == 0 ? "GET" : "SET"), channel, *value);
}
static void volumedb_callback(enum dmr_control_type type, const char* channel, int* value)
{
	printf("volumedb callback : type = %s , channel = %s, value = %d\n", (type == 0 ? "GET" : "SET"), channel, *value);
}
static void loudness_callback(enum dmr_control_type type, const char* channel, int* value)
{
	printf("loudness callback : type = %s , channel = %s, value = %d\n", (type == 0 ? "GET" : "SET"), channel, *value);
}
extern u8 *wpa_supplicant_get_mac(void);
int  CreateMediaRender(char * buf)
{
	tls_os_status_t status;
	u8 uuid[17] = {0};
	u8 *mac = wpa_supplicant_get_mac();
	sprintf((char *)uuid, "%02x%02x%02x%02x%02x%02x-dmr",
	mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
	tls_dmr_init((char *)uuid, (char *)uuid);

	tls_gpio_cfg(SPI_CLK, TLS_GPIO_DIR_OUTPUT, TLS_GPIO_ATTR_FLOATING);
	tls_gpio_cfg(SPI_DATO, TLS_GPIO_DIR_OUTPUT, TLS_GPIO_ATTR_FLOATING);
	tls_gpio_cfg(SPI_CCS, TLS_GPIO_DIR_OUTPUT, TLS_GPIO_ATTR_PULLHIGH);
	tls_gpio_cfg(DAT_REQ, TLS_GPIO_DIR_INPUT, TLS_GPIO_ATTR_FLOATING);
	tls_gpio_cfg(LED, TLS_GPIO_DIR_OUTPUT, TLS_GPIO_ATTR_PULLHIGH);
	tls_gpio_cfg(RST, TLS_GPIO_DIR_OUTPUT, TLS_GPIO_ATTR_PULLHIGH);

//	tls_gpio_cfg(RUN_MODE, TLS_GPIO_DIR_INPUT, TLS_GPIO_ATTR_FLOATING);

	SPI_CS(1);
	LED_ON(0);

	status = tls_os_queue_create(&sd_down_mbox,64);
	if (status != TLS_OS_SUCCESS) {
		return -1;
	}

	 tls_os_task_create(NULL, NULL,
                       sd_down_thread,
                       NULL,
                       (void *)sd_down_task_stk,
                       UPNP_SD_STK_SIZE * sizeof(u32),
                       DEMO_DMR_TASK_PRIO,//DEMO_DMR_TASK_PRIO,
                       0);

	tls_dmr_set_play_callback(httpdownloadmusic);
	tls_dmr_set_stop_callback(httpstopdownloadmusic);
	tls_dmr_set_seek_callback(httpstopdownloadmusic);
	tls_dmr_set_pause_callback(httpstopdownloadmusic);
	tls_dmr_set_play_progress_callback(get_grogress);

	tls_dmr_set_mute_callback(mute_callback);
	tls_dmr_set_volume_callback(volume_callback);
	tls_dmr_set_volumedb_callback(volumedb_callback);
	tls_dmr_set_loudness_callback(loudness_callback);
	return 0;
}


#endif

